// pages/wallet/wallet.js
var common = require("../../utils/public.js") //引入public.js脚本
const app = getApp();

var api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    list:[],
    balance: '',
    phone:'',
    amount:0,
    bottom: '',
  },
  //显示充值弹出框
  openit: common.throttleFunc(function (e) {
    this.setData({
      showModalStatus: 'on'
    })
  }),
  //隐藏充值弹出框
  hideModal: common.throttleFunc(function (e) {
    this.setData({
      showModalStatus: ''
    })
  }),
  getPhone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  getUser: common.throttleFunc(function (e) {
    wx.showLoading({
      title: '加载中...'
    })
    let that=this
    let phone=that.data.phone
    api.getUserByPhone({
      phone,
    }).then(res=>{
      if (res.result){
        if(res.object==null){
          wx.showToast({
            title: '查无此人，请重新输入',
            icon: 'none'
          })
          that.setData({
            phone:""
          })
          // wx.hideLoading()
        }else{
          
          var userMess = JSON.stringify(res.object);
          wx.navigateTo({
            url: '/pages/new1/new1?amount=' + that.data.amount + "&phone=" + that.data.phone + "&id=" + that.data.id + "&userMess=" + userMess,
          })
          that.setData({
            showModalStatus: '',
            phone: ""
          })
          wx.hideLoading()
        }
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    })
  }),
  onShow:function(){
    api.get_cart({
      userId: wx.getStorageSync("openId"),
      cityId: wx.getStorageSync("cityId"),
      page: {}
    }).then(res => {
      console.log(res)
      if (res.data.result) {
        wx.setStorageSync("userMess", res.data.object)
        this.setData({
          balance: res.data.object.balance
        })
      }

    })
    this.setData({
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //京钱充值
    let that=this
    this.setData({
      bottom: app.globalData.isFullSucreen ? '40rpx' : '0',
    })
    api.getAll({
      status: '1',
      cityId: wx.getStorageSync('cityId'),
    }).then(res => {
      if (res.result) {

     
          for (var i = 0; i < res.object.length; i++) {//遍历数组 给数组元素加上隐藏
            res.object[i].flag = true;
          }
          res.object[0].flag = false
          that.setData({
            list: res.object,
            id: res.object[0].id,
            amount: res.object[0].amount
          })
        
      }
    })


   
  },
  // 跳转下一页
  electronic: common.throttleFunc(function (e) {
    wx.navigateTo({
      url: '/pages/electronicBilling/electronicBilling',
    })
  }),
  // 充值按钮
  cz: common.throttleFunc(function (e) {
    let that=this
    api.topUp({
      id: that.data.id,
    }).then(res => {
      if (res.result == true) {
        wx.requestPayment({
          timeStamp: res.object.timeStamp,
          nonceStr: res.object.nonce_str,
          package: res.object.packageParams,
          signType: "MD5",
          paySign: res.object.sign,
          success: function (re) {
            api.topUpConfirm({
              orderCode: res.object.out_trade_no
            }).then(res => {
               console.log(res)
              api.get_cart({
                userId: wx.getStorageSync("openId"),
                cityId: wx.getStorageSync("cityId"),
                page: {}
              }).then(res => {
                if (res.data.result) {
                  wx.setStorageSync("userMess", res.data.object)
                  wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                  that.setData({
                    balance: res.data.object.balance
                  })
                }
              })
            })
          },
          fail: function (res) {
            console.log(res)
          }
        })
      }
    })
  
  }),

  //选择充值金额
  btnPirce:function(e){
   
    let that=this
    //获取充值数组
    let list=that.data.list
    //判断当前是否选择
    if (list[e.currentTarget.dataset.index].flag==false){
    } else if (list[e.currentTarget.dataset.index].flag == true){
      //遍历当前数组
      for (let i = 0; i < list.length; i++) {
        //判断数组是选中的 
        if (list[i].flag==false){
          list[i].flag = ! list[i].flag
        }
        //当前点击的选中
         list[e.currentTarget.dataset.index].flag = false
      }
      //从新遍历数组
      that.setData({
        list: list,
        id: e.currentTarget.dataset.id,
        amount: list[e.currentTarget.dataset.index].amount
      })
    }
  }
})